<script setup>
import FirstTitle from "@comp/FirstTitle";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { onMounted } from "vue";
import { searchDataZymb } from "@/api/search";
import { addOrUpdateZymb } from "@/api/addOrUpdate";

const city = window.globalObj.name;
const editorRef = shallowRef();

const toolbarConfig = {};
const editorConfig = {
  placeholder: "请输入内容...",
};
const mode = ref("default");

// 加载html
const initHtml = () => {
  searchDataZymb({ m: "组织指挥-指挥主体" }).then((res) => {
    const { data } = res;
    valueHtml.value = data.find((x) => x.k === "指挥主体")?.v || "";
  });
};

const handleCreated = (editor) => {
  editorRef.value = editor; // 记录 editor 实例，重要！
};

const valueHtml = ref(
  "由指挥控制中心的综合计划组、人员疏散隐蔽组、重要目标防护组、城市管控组、抢救抢修组，情报信息部的综合情报组、通信警报组、网电防护组，政治工作部的组织工作组、宣传动员组、心理防护组，综合保障部的组织计划组、工程保障组、卫生防疫组，防化保障组、交通运输保障组、物资保障组，统一组织指挥协调重要目标防护行动。<br/>1.指挥控制中心<br/>（1）综合计划和评估组<br/>（2）人员防护指挥组<br/>（3）重要目标防护指挥组<br/>（4）跨区支援指挥组<br/>（5）抢救抢修指挥组<br/>（6）临时指挥组<br/>2.情报信息部<br/>（1）综合情报组<br/>（2）警报通信组<br/>（3）信息防护组<br/>3.政治工作部<br/>（1）组织工作组<br/>（2）纪检监察组<br/>（3）宣传动员组<br/>（4）心理防护组<br/>4.综合保障部<br/>（1）保障计划组<br/>（2）工程保障组<br/>（3）医疗救护和防化防疫保障组<br/>（4）交通运输保障组<br/>（5）后勤物资器材保障组"
);

const saveProgramme = () => {
  ElMessageBox.confirm(`确定要保存吗?`, "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    customClass: "message-box",
  }).then(() => {
    addOrUpdateZymb({
      m: "组织指挥-指挥主体",
      data: [
        {
          k: "指挥主体",
          v: valueHtml.value,
        },
      ],
    })
      .then(() => {
        ElMessage.success("操作成功");
      })
      .catch(() => {});
  });
};

onMounted(() => {
  initHtml();
});
onBeforeUnmount(() => {
  const editor = editorRef.value;
  if (!editor) return;
  editor.destroy();
});
</script>
<template>
  <div class="">
    <FirstTitle name="指挥主体" />
    <div style="border: 1px solid #ccc" class="top-15">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef"
        :defaultConfig="toolbarConfig"
        :mode="mode"
      />
      <Editor
        style="height: 500px; overflow-y: hidden"
        v-model="valueHtml"
        :defaultConfig="editorConfig"
        :mode="mode"
        @onCreated="handleCreated"
      />
    </div>

    <div class="flex-justify-end top-20">
      <el-button type="primary" class="save-btn" @click="saveProgramme"
        >保存</el-button
      >
    </div>
  </div>
</template>
<style scoped lang="scss"></style>